package com.apps.ppt.service.impl;

import com.apps.ppt.config.PPtConfig;
import com.apps.ppt.service.PPtService;
import com.apps.ppt.service.model.vo.PPtInfoVo;
import com.apps.ppt.utils.PPtxContextHelper;
import com.apps.saspect.RunTimes;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.poi.sl.usermodel.Slide;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.sl.usermodel.SlideShowFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;

@Slf4j
@Service
public class PPtServiceImpl implements PPtService {

    @Autowired
    private PPtConfig pPtConfig;


    @RunTimes
    @SneakyThrows
    @Override
    public PPtInfoVo readPPt(File file, String thumb_name, String detail_name)
    {
        PPtInfoVo result=new PPtInfoVo();
        FileInputStream is = new FileInputStream(file);
        //读取幻灯片数组
        SlideShow ppt = SlideShowFactory.create(is);
        is.close();
        Dimension pageSize = ppt.getPageSize();
        //缩略图缓存
        BufferedImage thumbBuffer = null;
        //详情图缓存
        BufferedImage detailBuffer = null;
        for (int i=0;i< ppt.getSlides().size();i++)
        {
            Slide slide= (Slide) ppt.getSlides().get(i);
            BufferedImage img = new BufferedImage(pageSize.width*pPtConfig.getRead_images_rate(), pageSize.height*pPtConfig.getRead_images_rate(), BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = img.createGraphics();
            graphics.setPaint(Color.BLUE);
            graphics.scale(pPtConfig.getRead_images_rate(), pPtConfig.getRead_images_rate());
            graphics.fill(new Rectangle2D.Float(0, 0, pageSize.width*pPtConfig.getRead_images_rate(), pageSize.height*pPtConfig.getRead_images_rate()));
            slide.draw(graphics);
            if(null == thumbBuffer)
               thumbBuffer = new BufferedImage(img.getWidth(), (img.getHeight() + pPtConfig.getPageSpace())* ppt.getSlides().size(), BufferedImage.TYPE_INT_RGB);
            if(null == detailBuffer)
               detailBuffer = new BufferedImage(img.getWidth(), (img.getHeight() + pPtConfig.getPageSpace())* ppt.getSlides().size(), BufferedImage.TYPE_INT_RGB);
            //处理图片
            handleImages(img,thumbBuffer,detailBuffer,i);
        }
        //如果缩略图缓存大于允许高度则进行裁剪缩放,否则直接写入
        BufferedImage thumbSub=thumbBuffer.getHeight()>(pPtConfig.getThumb_height()*pPtConfig.getRead_images_rate())?thumbBuffer.getSubimage(0,0,thumbBuffer.getWidth(),(pPtConfig.getThumb_height()*pPtConfig.getRead_images_rate())):thumbBuffer;
        //缩放
        thumbBuffer = Thumbnails.of(thumbSub).scale(pPtConfig.getSave_images_scale(),pPtConfig.getSave_images_scale()).asBufferedImage();
        result.setThumbBuffer(thumbBuffer);
        result.setThumbPath(file.getParent()+"/"+thumb_name+"."+pPtConfig.getFormat());
        //保存缩略图
        ImageIO.write(thumbBuffer, pPtConfig.getFormat(), new File(result.getThumbPath()));
        result.setDetailBuffer(detailBuffer);
        result.setDetailPath(file.getParent()+"/"+detail_name+"."+pPtConfig.getFormat());
        //保存详情图
        ImageIO.write(Thumbnails.of(detailBuffer).scale(pPtConfig.getSave_images_scale(),pPtConfig.getSave_images_scale()).asBufferedImage(), pPtConfig.getFormat(), new File(result.getDetailPath()));
        return result;
    }

    /**
     * 处理图片缓存进行拼接等业务
     * @param bufferedImage 图片缓存源
     * @param thumbBuffer 缩略图缓存
     * @param detailBuffer 详情图缓存
     * @param index
     */
    private void handleImages(BufferedImage bufferedImage,BufferedImage thumbBuffer,BufferedImage detailBuffer,int index)
    {
            if(null == bufferedImage)
                return;
            int height = bufferedImage.getHeight();
            int width = bufferedImage.getWidth();
            //取出缓存中的数据构建一个RGB数组
            int[] imageRgbArray = new int[width * height];
                  imageRgbArray = bufferedImage.getRGB(0, 0, width, height, imageRgbArray, 0, width);
                //第一张图片最大
                if (index == 0) {
                    thumbBuffer.setRGB(0, (height + pPtConfig.getPageSpace()) * index, width, height, imageRgbArray, 0, width);
                } else {
                    //偶数计数器
                    int count = (index + 1) / 2;
                    //求出y
                    int y = (count <= 2 ? height : (height / 2) * count) + pPtConfig.getPageSpace();
                    //求出x
                    int x = index % 2 == 1 ? 0 : (width / 2) + pPtConfig.getPageSpace();
                    //缩放布局
                    Image img = bufferedImage.getScaledInstance((width / 2) - (pPtConfig.getPageSpace() / 2), (height / 2) - pPtConfig.getPageSpace(), Image.SCALE_DEFAULT);
                    //塞进画板
                    thumbBuffer.getGraphics().drawImage(img, x, y, null);
                }
            //合成详情图
            detailBuffer.setRGB(0,(height  + pPtConfig.getPageSpace()) * index,width,height,imageRgbArray,0,width);
    }

}
